<?php namespace App\Http\Services;

use App\Admin;
use App\User;
use Blocktrail\CryptoJSAES\CryptoJSAES;
use Carbon\Carbon;
use Auth;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
use Log;
use Cookie;

class AdminManager
{
    public static function isLogin(Request $request = null,$front = 0)
    {
        if (!isset($_COOKIE['admin_token'])) {
            if (is_null($request)) {
                return false;
            } else {
//                Log::error("header==>".print_r($request->header('Admin-token'),true));
                $token = !is_null($request->header('Admin-token')) ? $request->header('Admin-token') : $request->get('admin_token');
                if (!is_null($token)) {
                    $token = urldecode($token);

                    $userKey = false;
                    try {
                        $userKey = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
                    } catch (\Exception $e) {
                        return false;
                    }

                    if (!$userKey)
                        return false;

                    $thatUser = User::whereUserKey($userKey)->whereRole(1)->first();
                    if (!$thatUser){
                        return false;
                    }

                    return $thatUser;
                } else {
//                    Log::error("aaaa");
//                    if(Auth::guard()->guest()){
//                        Log::error("bbbb");
                    return false;
//                    }

//                    return Auth::guard()->user();
//                    return false;
                }
            }
        } else {
            $token = Cookie::get('admin_token');

            if (!is_null($token)) {

                $token = urldecode($token);

                $userKey = false;
                try {
                    $userKey = CryptoJSAES::decrypt(str_replace(" ", "+", $token), env("AES_KEY"));
                } catch (\Exception $e) {
                    return false;
                }

                if (!$userKey)
                    return false;

                $thatUser = User::whereUserKey($userKey)->whereRole(1)->first();
                if (!$thatUser) {
                    return false;
                }

                return $thatUser;
            }else{
                return false;
            }
        }
    }

    /**
     * 检查用户名是否已经存在或被使用
     * @param $username
     * @return bool
     */
    public static function isUsernameExists($username){
        if(Admin::whereUsername($username)->exists())
            return true;
        return false;
    }

    /**
     * 检查email是否已经存在或被使用
     * @param $email
     * @return bool
     */
    public static function isEmailExists($email){
        if(Admin::whereEmail($email)->exists())
            return true;
        return false;
    }

    /**
     * 通过用户名来登录
     * @param $username
     * @param $password
     * @param bool $remember
     * @return bool|Authenticatable|null
     */
    public static function signInByUsername($username, $password, $remember = false){
        if(!self::isUsernameExists($username))
            return false;

        if(Auth::guard('admin')->attempt(['username' => $username, 'password' => $password],$remember)){
            return Auth::guard('admin')->user();
        }

        return false;
    }
}
